// components/my/usermotto/index.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        info: {
            id: 0,
            img: '/images/doctor.png',
            name: '',
            hospital: '',
            section: '',
            title: '',
            year: '',
            goodAt: '',
            rate: '4.8',
            answer: 430,
            tags: ['从业12年', '快速回复'],
        },
        focus: {
            name: false,
            hospital: false,
            section: false,
            title: false,
            year: false,
            goodAt: false,
        },
        needInput: [{
            chinese: '姓名',
            english: 'name'
        }, {
            chinese: '医院',
            english: 'hospital'
        }, {
            chinese: '科室',
            english: 'section'
        }, {
            chinese: '职称',
            english: 'title'
        }, {
            chinese: '擅长',
            english: 'goodAt'
        }, {
            chinese: '从业年限',
            english: 'year'
        }, ],
        userInput: false,
        inputStorage: '',
        curKey: '',
    },

    //   组件初始化
    attached() {
        const info = wx.getStorageSync('info')
        if (info) {
            this.setData({
                info
            })
        }

    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 直接输入框方式
        // onInput(e) {
        //     console.log('====================================');
        //     console.log(e);
        //     console.log('====================================');
        //     const key = e.currentTarget.dataset.name
        //     const value = e.detail.value
        //     let info = this.data.info
        //     info[key] = value
        //     this.setData({
        //         info
        //     })
        //     // return 'sssss'
        // },
        // 在下方调出输入框的方式
        // item项点击 判断是哪一项并保存key
        onItemClick(e) {
            const key = e.currentTarget.dataset.name
            let focus = this.data.focus
            const curValue = focus[key]
            for(let v in focus) {
                focus[v] = false
            }
            focus[key] = !curValue
            this.setData({
                focus,
                inputStorage: '',
                curKey: key
            })
        },

        // goSetMedicalHis: function (e) {
        //     let userInput = !this.data.userInput
        //     this.setData({
        //         userInput
        //     })
        // },
        handleInputBlur: function (e) {
            const value = e.detail.value

            this.setData({
                inputStorage: value.replace(/\s+/g, "")
            })
        },
        handleBtnClick: function (e) {
            setTimeout(() => {
                const value = this.data.inputStorage
                let info = this.data.info
                let focus = this.data.focus
                const key = this.data.curKey
                info[key] = value
                focus[key] = false
                if (!value) {
                    this.setData({
                        focus
                    })
                    return
                }
                this.setData({
                    info,
                    focus,
                    userInput: false,
                    inputStorage: ''
                })
                wx.setStorageSync('info', info)
                // this.setStorage()
                // wx.showToast({
                //     title: '完成',
                //     icon: 'success'
                // })
            }, 200)

        },
        // 上传设置
        setStorage: function () {
            let profile = {}
            let userId = wx.getStorageSync('userId')
            profile.userid = userId
            profile.height = this.data.height
            profile.weight = this.data.weight
            profile.age = this.data.age
            profile.medicalHis = this.data.medicalHis
            profile.imgurl = this.data.userInfo.avatarUrl
            profile.realName = this.data.userInfo.nickName
            wx.setStorageSync('profile', profile)
            console.log('profile', profile)
            wx.showLoading({
                title: '上传设置中···',
            })
            let that = this
            wx.request({
                //user/updateprofile.json?userid=&height&weight&age&medicalHis&imgur
                url: app.globalData.host + 'user/updateprofile.json',
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                data: profile,
                success: function (res) {
                    console.log('回包', res) // 服务器回包内容
                    if (res.data.data) {
                        // that.formatData(res.data.data)
                        console.log('上传成功', res)
                        wx.showToast({
                            title: '上传成功!',
                            icon: 'success'
                        })
                    }
                },
                fail: function (res) {
                    console.log('上传失败', res)
                    wx.showToast({
                        title: '上传失败!',
                        icon: 'none'
                    })
                },
                complete: function (res) {
                    wx.hideLoading()
                }
            })
        },
    }
})